The number of the single character at index (0-based) - string_char_at's partner: char_at gives the character as text, code_at gives it as a number ("A" is 65, "a" is 97, "0" is 48).
string_code_at(s, index) -> int| Name | Type |
|---|---|
s | string |
index | int |
int
The number of the single character at index: string_code_at("A", 0) is 65. It is string_char_at's partner - string_char_at hands the character back as text, string_code_at hands it back as a number. Positions count from 0, the same for both.
Every letter, digit and mark has its own number ("A" is 65, "a" is 97, "0" is 48, a space is 32). It is the same number the < and > comparisons use to put text in order, which is why every capital letter comes before every lowercase one.
The one difference from string_char_at is what an index outside the text gives. string_char_at gives empty text. A number has no "empty", so string_code_at gives -1, and no real character is ever -1 - they run from 0 to 255 - so string_code_at(s, i) == -1 safely means "nothing there". It never crashes, and it makes no new text, so there is nothing to cut off.
var name = "R2D2";
var first = string_code_at(name, 0); // 82, the number of "R"
var digit_value = string_code_at(name, 1) - 48; // 2, the value of the digit "2"
var is_digit = string_code_at(name, 2) < 48 || string_code_at(name, 2) > 57; // true: "D" is not a digit
var past_end = string_code_at(name, 4); // -1Subtracting 48 turns a digit character into the number it spells, because the digits "0" to "9" are numbered 48 to 57 in order.
Counts from 0, exactly like string_char_at. The one difference is what an index out of range (either side) gives back: string_char_at gives an empty string, string_code_at gives -1, because a number has no "empty" - and no real character is ever -1 (they run 0 to 255), so `string_code_at(s, i) == -1` safely means "nothing there". It is the same number `<` and `>` compare by, so `string_code_at(s, i) - 48` is the value of a digit character. Never builds a string, so it cannot run out of room.